package cn.lili.modules.order.order;

import cn.lili.common.vo.PageVO;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.order.order.entity.dto.OrderSearchParams;
import cn.lili.modules.order.order.mapper.OrderMapper;
import cn.lili.modules.order.order.service.OrderService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 收货地址业务层实现
 *
 * @author Chopper
 * @since 2020/11/18 9:44 上午
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderTempServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderTempService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderService orderService;

    @Override
    public IPage<Order> getOrderList(PageVO page, String orderType) {


        if (StringUtils.isBlank(orderType)) {
            if (StringUtils.isBlank(page.getStoreId())) {
                return this.page(PageUtil.initPage(page),
                        new QueryWrapper<Order>().eq("pay_status", "PAID"));
            } else {
                return this.page(PageUtil.initPage(page),
                        new QueryWrapper<Order>()
                                .eq("store_id", page.getStoreId())
                                .eq("pay_status", "PAID"));
            }

        } else {
            return this.page(PageUtil.initPage(page),
                    new QueryWrapper<Order>()
                            .eq("consignee_detail", orderType));
        }

    }


    @Override
    public void updateOrder(String sn, String status) {

        orderMapper.updateStatus(sn, status);

    }

    @Override
    public Order getOrderInfo(String sn) {
        OrderSearchParams searchParams = new OrderSearchParams();
        searchParams.setOrderSn(sn);
        List<Order> orders = orderService.queryListByParams(searchParams);
        if (CollectionUtils.isEmpty(orders)) {
            return null;
        }
        return orders.get(0);
    }
}